import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)
// /layout/home
const routes = [
  {
    path: '/',
    redirect: '/layout',
  },

  {
    path: '/wangEdit',
    name: 'WangEdit',
    component: () => import(/* webpackChunkName: "about" */ '../views/wangEdit/index.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login/index.vue'),
  },
  // 发布页面
  {
    path: '/release',
    name: 'Release',
    component: () => import(/* webpackChunkName: "about" */ '../views/A-Release/index.vue'),
  },
  // 已经发布页面
  {
    path: '/all-release',
    name: 'AllRelease',
    component: () => import(/* webpackChunkName: "about" */ '../views/A-all-release/index.vue'),
  },
  // 草稿页面
  {
    path: '/draft',
    name: 'draft',
    component: () => import(/* webpackChunkName: "about" */ '../views/A-draft/index.vue'),
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import('../views/detail/index.vue'),
  },
  {
    path: '/layout',
    name: 'Layout',
    component: () => import(/* webpackChunkName: "about" */ '../layout/index.vue'),
    redirect: '/layout/home',
    children: [
      {
        path: 'Vmodel',
        name: 'Vmodel',
        component: () => import(/* webpackChunkName: "about" */ '../views/v-model/index.vue'),
      },
      {
        path: 'pullup',
        name: 'PullUp',
        component: () => import(/* webpackChunkName: "about" */ '../views/pullup/index.vue'),
      },
      {
        path: 'home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "about" */ '../views/home/index.vue'),
      },

      {
        path: 'buycar',
        name: 'BuyCar',
        component: () => import(/* webpackChunkName: "about" */ '../views/buyCar/index.vue'),
      },
      {
        path: 'sellcar',
        name: 'SellCar',
        component: () => import(/* webpackChunkName: "sellcar" */ '../views/sellCar/index.vue'),
      },
      {
        path: 'collect',
        name: 'Collect',
        component: () => import(/* webpackChunkName: "collect" */ '../views/collect/index.vue'),
      },
      {
        path: 'mine',
        name: 'Mine',
        component: () => import(/* webpackChunkName: "about" */ '../views/mine/index.vue'),
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

// 导航守卫：判断用户是否已经登陆，如果登陆，就让用户进入其他页面

router.beforeEach((to, from, next) => {
  // console.log(to, 'to')
  // 用户如果想进入登陆页面，直接让他走，
  if (to.name === 'Login') {
    next()
  } else {
    const token = store.state.token
    if (token) {
      next()
    } else {
      next({
        path: '/login',
        query: { fullpath: to.fullPath },
      })
    }
  }
})

export default router
